package tool;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.HttpStatus;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import constant.ConfigConstants;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.DatePicker;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.scene.web.WebView;
import javafx.stage.Stage;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

/**
 * 星座
 * Created by Intellij IDEA.
 * User:  老赵
 * Date:  2023/7/5
 */
public class Constellation {

    public static void openConstellationStage() {
        Stage constellationStage = new Stage();
        constellationStage.setTitle("生肖星座查询");
        constellationStage.setFullScreen(ConfigConstants.FullScreen);

        // 创建时间选择器和按钮
        Label cityLabel = new Label("出生年月日:");
        DatePicker datePicker = new DatePicker();
        Button calculateButton = new Button("生肖星座");
        // 创建一个WebView组件
        Label webViewLabel = new Label("生肖星座查询结果:");
        WebView webView = new WebView();
        webView.setPrefSize(500, 650);
        calculateButton.setOnAction(event -> {
            LocalDate selectedDate = datePicker.getValue();
            String result = calculateConstellation(selectedDate);
            webView.getEngine().loadContent(result);
        });
        VBox root = new VBox(10);
        root.setPadding(new Insets(10));
        root.getChildren().addAll(cityLabel, datePicker, calculateButton, webViewLabel, webView);
        Scene scene = new Scene(root, 500, 700);
        constellationStage.setScene(scene);
        constellationStage.show();
    }

    private static String calculateConstellation(LocalDate date) {
        if (ObjectUtil.isEmpty(date)) {
            return "请选择出生日期";
        }
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String url = "https://zj.v.api.aa1.cn/api/Age-calculation/?birthday=" + date.format(formatter);
        String html = HttpUtil.get(url);
        String result;
        JSONObject jsonObject = JSONUtil.parseObj(html);
        StringBuilder sb = new StringBuilder();
        sb.append("<div style=\"background-color: #f2f2f2; padding: 10px;\">")
                .append("<h3>生肖星座信息：</h3>")
                .append("<div>")
                .append("<ul style=\"list-style-type: none; padding: 0;\">");
        if (HttpStatus.HTTP_OK == jsonObject.getInt("code")) {
            sb.append("<li style=\"list-style-type: none; padding: 0;\">")
                    .append("<strong>年龄：</strong>").append(jsonObject.getStr("age")).append("<br/>")
                    .append("<strong>生肖：</strong>").append(jsonObject.getStr("animal")).append("<br/>")
                    .append("<strong>星座：</strong>").append(jsonObject.getStr("constellation")).append("<br/>")
                    .append("</li>");
            result = sb.toString();
        } else {
            sb.append("<li style=\"margin-bottom: 10px;\">")
                    .append("查询出错").append("<br/>")
                    .append("</li>");
            result = sb.toString();
        }
        return result;
    }
}
